package com.realife.eson.common.util.jwt;

import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.joda.time.DateTime;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/4/14 16:01
 * @description:
 */
@SuppressWarnings("all")
public class JWTHelper {
    private static RsaKeyHelper rsaKeyHelper = new RsaKeyHelper();

    public static String generateToken(ITokenInfo jwtInfo, String priKeyPath, int expire) throws Exception {
        return Jwts.builder().setSubject(jwtInfo.getUniqueName()).claim("accountId", jwtInfo.getAccountId()).claim("name", jwtInfo.getName()).setExpiration(DateTime.now().plusSeconds(expire).toDate()).signWith(SignatureAlgorithm.RS256, rsaKeyHelper.getPrivateKey(priKeyPath)).compact();
    }

    public static String generateTokenCore(ICoreJWTInfo jwtInfo, byte[] priKey, int expire) throws Exception {
        return Jwts.builder().setSubject(jwtInfo.getAutId()).claim("systemId", jwtInfo.getSysId()).setExpiration(DateTime.now().plusSeconds(expire).toDate()).signWith(SignatureAlgorithm.RS256, rsaKeyHelper.getPrivateKey(priKey)).compact();
    }

    public static String generateToken(ITokenInfo jwtInfo, byte[] priKey, int expire) throws Exception {
        return Jwts.builder().setSubject(jwtInfo.getUniqueName()).claim("accountId", jwtInfo.getAccountId()).claim("name", jwtInfo.getName()).claim("sessionId", jwtInfo.getSessionId()).setExpiration(DateTime.now().plusSeconds(expire).toDate()).signWith(SignatureAlgorithm.RS256, rsaKeyHelper.getPrivateKey(priKey)).compact();
    }

    public static Jws<Claims> parserToken(String token, String pubKeyPath) throws Exception {
        return Jwts.parser().setSigningKey(rsaKeyHelper.getPublicKey(pubKeyPath)).parseClaimsJws(token);
    }

    public static Jws<Claims> parserToken(String token, byte[] pubKey) throws Exception {
        return Jwts.parser().setSigningKey(rsaKeyHelper.getPublicKey(pubKey)).parseClaimsJws(token);
    }

    public static ITokenInfo getInfoFromToken(String token, String pubKeyPath) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKeyPath);
        Claims body = (Claims)claimsJws.getBody();
        return new JWTInfo(body.getSubject(), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("accountId"))), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("name"))), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("sessionId"))), body.getExpiration());
    }

    public static ITokenInfo getInfoFromToken(String token, byte[] pubKey) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKey);
        Claims body = (Claims)claimsJws.getBody();
        return new JWTInfo(body.getSubject(), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("accountId"))), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("name"))), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("sessionId"))), body.getExpiration());
    }

    public static ICoreJWTInfo getInfoFromTokenCore(String token, byte[] pubKey) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKey);
        Claims body = (Claims)claimsJws.getBody();
        return new CoreJWTInfo(body.getSubject(), StrUtil.nullToEmpty(StrUtil.utf8Str(body.get("systemId"))), body.getExpiration());
    }

    private JWTHelper() {
    }
}

